package com.taotao.rest.service.ipml;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.JsonUtils;
import com.taotao.mapper.TbItemDescMapper;
import com.taotao.pojo.TbItemDesc;
import com.taotao.rest.dao.IJedisClient;
import com.taotao.rest.service.IItemDescService;

/**
 * 商品描述信息
 * 
 * @author Administrator
 *
 */
@Service
public class ItemDescServiceIpml implements IItemDescService {
	@Autowired
	private TbItemDescMapper itemDescMapper;

	@Autowired
	private IJedisClient jedisClient;

	@Value("${REDIS_ITEM_KEY}")
	private String REDIS_ITEM_KEY;

	@Value("${REDIS_ITEM_EXPIRE}")
	private Integer REDIS_ITEM_EXPIRE;

	/**
	 * 获取商品描述信息
	 */
	@Override
	public TaotaoResult getItemDescById(Long itemId) {
		// TODO Auto-generated method stub
		// 获取redis中的内容
		try {
			String str = this.jedisClient.get(REDIS_ITEM_KEY + ":" + itemId
					+ ":desc");
			if (StringUtils.isNotBlank(str)) {
				TbItemDesc itemDesc = JsonUtils.jsonToPojo(str,
						TbItemDesc.class);
				return TaotaoResult.ok(itemDesc);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		TbItemDesc itemDesc = this.itemDescMapper.selectByPrimaryKey(itemId);

		try {
			String jsonStr = JsonUtils.objectToJson(itemDesc);
			this.jedisClient.set(REDIS_ITEM_KEY + ":" + itemId + ":desc",
					jsonStr);
			this.jedisClient.expire(REDIS_ITEM_KEY + ":" + itemId + ":desc",
					REDIS_ITEM_EXPIRE);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return TaotaoResult.ok(itemDesc);
	}
}
